package com.xhcl.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.xhcl.common.exception.BusinessException;
import com.xhcl.system.domain.TSrhsAccountTemp;
import com.xhcl.system.domain.TSrhsDwcx;
import com.xhcl.system.mapper.TSrhsAccountTempMapper;
import com.xhcl.system.mapper.TSrhsDwcxMapper;
import com.xhcl.system.mapper.TSrhsPztoMapper;
import com.xhcl.system.model.ChangeHorizontal;
import com.xhcl.system.service.ITSrhsAccountTempService;

/**
 * 账务生成临时数据Service业务层处理
 * 
 * @author xhcl
 * @date 2020-09-01
 */
@Service
public class TSrhsAccountTempServiceImpl implements ITSrhsAccountTempService 
{
    @Autowired
    private TSrhsAccountTempMapper tSrhsAccountTempMapper;
    
    @Autowired
    private TSrhsDwcxMapper tSrhsDwcxMapper;
    
   
    /**
     * 查询账务生成临时数据
     * 
     * @param sjlb 账务生成临时数据ID
     * @return 账务生成临时数据
     */
    @Override
    public TSrhsAccountTemp selectTSrhsAccountTempById(Long sjlb)
    {
        return tSrhsAccountTempMapper.selectTSrhsAccountTempById(sjlb);
    }

    /**
     * 查询账务生成临时数据列表
     * 
     * @param tSrhsAccountTemp 账务生成临时数据
     * @return 账务生成临时数据
     */
    @Override
    public List<TSrhsAccountTemp> selectTSrhsAccountTempList(TSrhsAccountTemp tSrhsAccountTemp)
    {
        return tSrhsAccountTempMapper.selectTSrhsAccountTempList(tSrhsAccountTemp);
    }
    
    public void callDwdzFunction(Map map) {
    	 tSrhsAccountTempMapper.callDwdzFunction(map);
    }

    @Transactional
    public List<TSrhsAccountTemp> selectSortAccount(Map map) throws BusinessException{
   	   tSrhsAccountTempMapper.callSortAccount(map);
   	   if (!(map.get("outrev")!=null && map.get("outrev").equals("ok"))) {
   		   throw new BusinessException(map.get("outrev").toString());
   	   }
	   List<TSrhsAccountTemp> list=tSrhsAccountTempMapper.selectTSrhsAccountTempList(null);
	   return list;
   }
    @Transactional
    public List<TSrhsAccountTemp> selectBankAccount(Map map) throws BusinessException{
   	   tSrhsAccountTempMapper.callBankAccount(map);
   	   if (!(map.get("outrev")!=null && map.get("outrev").equals("ok"))) {
   		   throw new BusinessException(map.get("outrev").toString());
   	   }
	   List<TSrhsAccountTemp> list=tSrhsAccountTempMapper.selectTSrhsAccountTempList(null);
	   return list;
   } 
    
    @Transactional
    public List<TSrhsAccountTemp> selectInfoAccount(Map map) throws BusinessException{
   	   tSrhsAccountTempMapper.callInfoAccount(map);
   	   if (!(map.get("outrev")!=null && map.get("outrev").equals("ok"))) {
   		   throw new BusinessException(map.get("outrev").toString());
   	   }
	   List<TSrhsAccountTemp> list=tSrhsAccountTempMapper.selectTSrhsAccountTempList(null);
	   return list;
   } 
    
    @Transactional
    public List<TSrhsAccountTemp> selectFzmxAccount(Map map) throws BusinessException{
   	   tSrhsAccountTempMapper.callFzmxAccount(map);
   	   if (!(map.get("outrev")!=null && map.get("outrev").equals("ok"))) {
   		   throw new BusinessException(map.get("outrev").toString());
   	   }
	   List<TSrhsAccountTemp> list=tSrhsAccountTempMapper.selectTSrhsAccountTempList(null);
	   return list;
   }
    
    @Transactional
    public List<TSrhsAccountTemp> selectcallZybb(Map map) throws BusinessException{
   	   tSrhsAccountTempMapper.callZybb(map);
   	   if (!(map.get("outrev")!=null && map.get("outrev").equals("ok"))) {
   		   throw new BusinessException(map.get("outrev").toString());
   	   }
	   List<TSrhsAccountTemp> list=tSrhsAccountTempMapper.selectTSrhsAccountTempList(null);
	   return list;
   } 
    
    @Transactional
    public List<TSrhsAccountTemp> callZxjjReport(Map map) throws BusinessException {
       tSrhsAccountTempMapper.callZxjjReport(map);
   	   if (!(map.get("outrev")!=null && map.get("outrev").equals("ok"))) {
   		   throw new BusinessException(map.get("outrev").toString());
   	   }
	   List<TSrhsAccountTemp> list=tSrhsAccountTempMapper.selectTSrhsAccountTempList(null);
	   return list;
   } 
    
    @Transactional
    public List<TSrhsAccountTemp> callDwdzd(Map map) throws BusinessException {
       tSrhsAccountTempMapper.callDwdzd(map);
   	   if (!(map.get("outrev")!=null && map.get("outrev").equals("ok"))) {
   		   throw new BusinessException(map.get("outrev").toString());
   	   }
	   List<TSrhsAccountTemp> list=tSrhsAccountTempMapper.selectTSrhsAccountTempList(null);
	   return list;
   } 
   
    
    @Transactional
    @Override
    public List<ChangeHorizontal> callDwcx(final Map map) throws BusinessException {
    	List<ChangeHorizontal> cList=new ArrayList<ChangeHorizontal>();
        this.tSrhsAccountTempMapper.callDwcx(map);
        if (map.get("outrev") == null || !map.get("outrev").equals("ok")) {
            throw new BusinessException(map.get("outrev").toString());
        }
        String zbh=map.get("zbh").toString();
        List<TSrhsAccountTemp> dwcxinfoList=tSrhsAccountTempMapper.selectDwcxInfo(zbh);
        if (CollectionUtils.isEmpty(dwcxinfoList)){
        	throw new BusinessException("单位查询：单位、对应项金额数量为空");
        }
        Map info=new HashMap();
        dwcxinfoList.forEach(item->{
        	info.put(item.getDwbm()+"-"+item.getXh(), item.getQcye());
        });
        LinkedHashMap linkmap = new LinkedHashMap();
        TSrhsDwcx srhsDwcx=new TSrhsDwcx();
        srhsDwcx.setZbh(Long.valueOf(zbh));
        List<TSrhsDwcx> dwcxlist=tSrhsDwcxMapper.selectTSrhsDwcxList(srhsDwcx);
        dwcxlist.forEach(item->{
        	if (item.getSfxs()==1L) {
        		linkmap.put(String.valueOf(item.getXh()), new BigDecimal("0"));
        	}
        });
        if (linkmap.isEmpty()) {
        	throw new BusinessException("单位查询：要显示的信息为空");
        }
        List<TSrhsAccountTemp> adwcxList=tSrhsAccountTempMapper.selectDwcxDw();
        adwcxList.forEach(item->{
        	ChangeHorizontal ch=new ChangeHorizontal();
        	ch.setDwbm(item.getDwbm());
        	ch.setDwmc(item.getDwmc());
        	ch.setMap(linkmap);
        	cList.add(ch);
        });
        for (int k = 0; k < cList.size(); k++) {
            ChangeHorizontal e = (ChangeHorizontal)cList.get(k);
            Iterator i = e.getMap().keySet().iterator();
            while (i.hasNext()) {
              String key = e.getDwbm() + "-" + i.next().toString();
              if (info.containsKey(key)) { 
                  e.getMap().put(key.split("-")[1], (BigDecimal)info.get(key)); 
              }
            }
        }
        LinkedHashMap<String,BigDecimal> tolmap = new LinkedHashMap();
        for (int k = 0; k < cList.size(); k++) {
      	  ChangeHorizontal e = (ChangeHorizontal)cList.get(k);
      	  Iterator it = e.getMap().entrySet().iterator();
  	          while (it.hasNext()) {
          	  Entry<String, BigDecimal> entry = (Entry<String, BigDecimal>) it.next();
          	  String key = entry.getKey();
  	    	  if (k==0) {
  	          	//为0直接符值为第一个list中的值
  	          	tolmap.put(key, e.getMap().get(key));
  	          }
  	          else {
  	          	tolmap.put(key,tolmap.get(key).add(e.getMap().get(key)));	
  	          }  
  	       }
        }
        if (cList.size()>0) {
  	      ChangeHorizontal totalchg=new  ChangeHorizontal();
  	      totalchg.setDwbm("");
  	      totalchg.setDwmc("合计");
  	      totalchg.setMap(tolmap);
  	      cList.add(totalchg);
        }
        return cList;
    }
    
    /**
     * 查询账名号
     * @param zbh
     * @return
     */
    public List<TSrhsDwcx> selectTSrhsDwcxList(String zbh){
    	TSrhsDwcx srhsDwcx=new TSrhsDwcx();
        srhsDwcx.setZbh(Long.valueOf(zbh));
        List<TSrhsDwcx> dwcxlist=tSrhsDwcxMapper.selectTSrhsDwcxList(srhsDwcx);
        return dwcxlist;
    }
}
